package com.lanren.familybookapp.ui.user.view;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lanren.common.helper.InputTextHelper;
import com.lanren.common.interfaces.DialogClickListener;
import com.lanren.common.utils.EditTextUtils;
import com.lanren.common.utils.Utils;
import com.lanren.common.widget.ClearEditText;
import com.lanren.common.widget.TitleBarView;
import com.lanren.common.widget.statusbar.ImmersionBar;
import com.lanren.familybookapp.R;
import com.lanren.familybookapp.base.BaseUiActivity;
import com.lanren.familybookapp.ui.user.interfaces.IRegisterModel;
import com.lanren.familybookapp.ui.user.presenter.RegisterPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ClassName RegisterActivity
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/10/4 11:40
 * @Version 1.0
 */
public class RegisterActivity extends BaseUiActivity<RegisterPresenter> implements IRegisterModel.IRegisterView {

    @BindView(R.id.register_tip)
    TextView registerTip;
    @BindView(R.id.user_name)
    ClearEditText userName;
    @BindView(R.id.password)
    ClearEditText password;
    @BindView(R.id.verify_password)
    ClearEditText verifyPassword;
    @BindView(R.id.register)
    Button register;
    @BindView(R.id.title_view)
    TitleBarView titleView;

    @Override
    public RegisterPresenter createPresenter() {
        return new RegisterPresenter();
    }

    @Override
    protected void setStatusBar() {
        mImmersionBar = ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_register;
    }


    @Override
    public void doCreate(Bundle savedInstanceState) {
        initData();
    }

    @Override
    public void initView() {

    }

    public void initData() {
        EditTextUtils.setEditTextFilter(userName);
        new InputTextHelper.Builder(this)
                .setMain(register)
                .addView(userName)
                .addView(password)
                .addView(verifyPassword)
                .build();
    }

    @Override
    public void initEvent() {

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void showMessage(String message) {
        registerTip.setVisibility(View.VISIBLE);
        showComplete();
        registerTip.setTextColor(Color.parseColor("#ff0000"));
        registerTip.setText("提示：" + message);
    }

    @Override
    public void onSuccess() {
        registerTip.setVisibility(View.INVISIBLE);
        showComplete();
        showMessageDialog("注册成功,去登陆", true, new DialogClickListener() {
            @Override
            public void onCancelClick(View v) {

            }

            @Override
            public void onConfirmClick(View v) {
                startActivityFinish(LoginActivity.class);
            }
        });
    }


    @OnClick({R.id.title_view, R.id.register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_view:
                finish();
                break;
            case R.id.register:
                if (Utils.isFastClick(view)) {
                    mPresenter.register(userName.getText().toString(), password.getText().toString(), verifyPassword.getText().toString());
                }
                break;
        }
    }
}
